<template>
	<view class="container">
		<!-- 听写任务卡片 -->
		<view class="task-header">
			<text class="task-title">听写任务</text>
		</view>
		<view class="task-card">
			<view class="task-content">
				<view class="task-info">
					<view class="info-item">
						<text class="info-label">名称:</text>
						<text class="info-value">{{time}}-听写</text>
					</view>
					<view class="info-item">
						<text class="info-label">时间:</text>
						<text class="info-value">{{time}}</text>
					</view>
					<view class="info-item">
						<text class="info-label">老师:</text>
						<text class="info-value">{{teacherName}}</text>
					</view>
				</view>
				<view class="task-icon">
					<image src="/static/home/bgtime.png" class="icon-img" mode="aspectFit"></image>
				</view>
			</view>
		</view>

		<!-- 班级选择 -->
		<view class="section">
			<view class="section-title">班级选择</view>
			<view class="class-selection">

				<view v-for="(item,index) in myClassList" :key="index" :class="{'class-btn-1':setAct==index}"
					class="class-btn" @tap="selectClass(item,index)">
					<image src="/static/home/prop2.png" class="class-icon" mode="aspectFit"></image>
					<text class="class-text">{{item.className}}</text>
				</view>

			</view>
		</view>

		<!-- 听写答案编辑 -->
		<view class="section">
			<view class="section-title">听写答案编辑</view>

			<!-- 拍照上传 -->
			<view class="upload-section f-j-b f-a-c">
				<view class="upload-btn" @tap="uploadPhoto">
					<image src="/static/home/paishe.png" class="upload-icon" mode="aspectFit"></image>
					<text class="upload-text">拍照上传</text>
				</view>
				<view class="add-btn" @tap="addAnswer">
					<text class="add-text">+</text>
				</view>
			</view>



			<view class="">
				<dragSort :list.sync="list" label="name" :columnNum="4" :columnSpace="20" :rowHeight="60"
					:rowSpace="20"></dragSort>

				<!-- 	<view>排序后的id顺序如下：</view>
				<view>{{list.map(item => item.id).join('，')}}</view> -->

			</view>
		</view>

		<!-- 创建按钮 -->
		<view class="create-section">
			<button class="create-btn" @tap="createTask">创建听写任务</button>
		</view>
		<u-popup mode="center" border-radius="30" v-model="show">
			<view class="popW">
				<view class="top">
					<view class="tit">
						{{time}}-听写
					</view>
					<view class="botR">
						{{setObj.className}}
					</view>
				</view>
				<view class="boxF">
					<view class="tinD">
						听写答案
					</view>
					<view style="width: 100%;" class="f f-w-w ">
						<view v-for="(item,index) in list" :key="index" class="fcen ">
							{{item.name}}
						</view>
					</view>
					<view class="btn f f-a-c f-j-c">
						<view @click="show=false" class="lefbtn f f-a-c f-j-c">
							继续编辑
						</view>
						<view @click="tijiao" class="rig f f-a-c f-j-c">
							好的
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<u-popup mode="center" border-radius="30" v-model="showInp">
			<view style="width: 550rpx;" class="popW">
				<view class="top">
					<view class="tit">
						请输入词语
					</view>

				</view>
				<view class="boxF">
					<view class="tinD">
						<input type="text" placeholder="请输入词语" v-model="inputStr" class="inputStr" />
					</view>

					<view class="btn f f-a-c f-j-c">
						<view @click="showInp=false" class="lefbtn f f-a-c f-j-c">
							取消
						</view>
						<view @click="queren" class="rig f f-a-c f-j-c">
							确认
						</view>

					</view>
				</view>
			</view>
		</u-popup>


	</view>
</template>

<script>
	import dragSort from '@/components/dragSort/dragSort.vue'
	import {
		listFlot,
		listMap
	} from '@/uits/index.js'
	export default {
		components: {
			dragSort
		},
		data() {
			return {
				teacherName: uni.getStorageSync('teacherName'),
				show: false,
				showInp: false,
				time: '',
				setObj: {},
				setAct: -1,
				selectedClass: '',


				dragStartIndex: -1,
				isDragging: false,
				img: 'https://wx2.sinaimg.cn/orj360/005NpmmLly1g4zr1456bkj302r02kwe9.jpg',
				// 对应需要显示的标题
				props: {
					label: 'label'
				},
				// list: [{
				// 	name: '嫉妒',
				// 	id: 1
				// }, {
				// 	name: '疑惑',
				// 	id: 2
				// }, {
				// 	name: '探听',
				// 	id: 3
				// }, {
				// 	name: '呐喊',
				// 	id: 4
				// }, {
				// 	name: '繁忙',
				// 	id: 5
				// }, {
				// 	name: '特殊',
				// 	id: 6
				// }, {
				// 	name: '慈祥',
				// 	id: 7
				// }, {
				// 	name: '放肆',
				// 	id: 8
				// }, {
				// 	name: '沉寂',
				// 	id: 9
				// }, {
				// 	name: '挺脱',
				// 	id: 10
				// }, {
				// 	name: '汉语',
				// 	id: 11
				// }, {
				// 	name: '耳环',
				// 	id: 12
				// }],

				list: [{
					name: '芦花',
					id: 1
				}, {
					name: '发愣',
					id: 2
				}, {
					name: '铅笔',
					id: 3
				}, {
					name: '摇晃',
					id: 4
				}, {
					name: '枪栓',
					id: 5
				}, {
					name: '胳膊',
					id: 6
				}, {
					name: '劫难',
					id: 7
				}, {
					name: '鬼脸',
					id: 8
				}, {
					name: '戒指',
					id: 9
				}, {
					name: '绸子',
					id: 10
				}, {
					name: '敌人',
					id: 11
				}, {
					name: '尸首',
					id: 12
				}, {
					name: '防备',
					id: 13
				}, {
					name: '慌忙',
					id: 14
				}, {
					name: '战场',
					id: 15
				}, {
					name: '持续',
					id: 16
				}, {
					name: '命令',
					id: 17
				}, {
					name: '占领',
					id: 18
				}, {
					name: '射击',
					id: 19
				}, {
					name: '突击',
					id: 20
				}, {
					name: '枪弹',
					id: 21
				}, {
					name: '愤怒',
					id: 22
				}, {
					name: '注视',
					id: 23
				}, {
					name: '光荣',
					id: 24
				}, {
					name: '艰巨',
					id: 25
				}, {
					name: '消息',
					id: 26
				}, {
					name: '爆炸',
					id: 27
				}, ],
				myClassList: [],
				inputStr: ''
			}
		},
		onShow() {
			this.init()
		},
		onLoad() {
			this.time = uni.$u.timeFormat(this.timestamp, 'mm-dd hh:MM:ss');
		},
		methods: {
			queren() {
				if (this.inputStr.length == 0) {
					return this.$msg('请输入内容')
				}
				this.list.push({
					name: this.inputStr,
					id: +new Date()
				})
				this.showInp = false
				this.inputStr = ''
			},
			// 获取班级信息
			async init() {
				let teacher = await this.$api.classList({
					teacherId: uni.getStorageSync('teacherId')
				})
				if (teacher && teacher.data) {
					this.myClassList = teacher.data
				}
			},
			SortChange(e) {
				console.log(e)
			},
			// 点击提交任务
			tijiao() {
				if (this.setAct == -1) {
					this.$msg('请先选择班级')
					return
				}

				if (this.list.length == 0) {
					this.$msg('请先上传图片识别内容')
					return
				}

				let list = listMap(this.list)
				console.log('提交的听写内容:', list);

				// 显示提交中
				uni.showLoading({
					title: '创建中...'
				});

				this.$api.createTingxie({
					"taskTitle": `${this.time}-听写`, // 任务标题
					"taskType": "dictate", // 听写任务类型
					"teacherId": uni.getStorageSync('teacherId'), // 老师ID
					"classId": this.setObj.classId, // 选择的班级ID
					"taskContent": JSON.stringify(list), // 识别内容
					"schoolId": uni.getStorageSync('schoolId')
				}).then(res => {
					uni.hideLoading();
					if (res && res.code == '0000') {
						this.$msg('创建成功')
						this.show = false
						// 清空数据
						setTimeout(() => {
							this.list = []
							this.setAct = -1
							this.setObj = {}
							// 跳转首页
							uni.switchTab({
								url: '/pages/index/index'
							})
						}, 1000)
					} else {
						this.$msg('创建失败，请重试')
					}
				}).catch(error => {
					console.error('创建任务失败:', error);
					uni.hideLoading();
					this.$msg('创建失败，请重试')
				})
			},
			selectClass(item, index) {

				this.setAct = index
				this.setObj = item

			},
			uploadFile(tempFilePath) {
				uni.uploadFile({
					url: 'http://118.178.189.237:38080/api/v1/task/ocr', // 上传接口地址
					filePath: tempFilePath, // 临时文件路径
					name: 'image', // 后端接收的字段名（如 `file`）
					formData: {},
					success: (res) => {
						console.log("上传成功:", res.data);
						// 如果后端返回 JSON，可能需要手动解析
						try {
							const data = JSON.parse(res.data);
							this.list = listFlot(JSON.parse(data.data));
							console.log("解析后的数据:", this.list);
						} catch (e) {
							console.log("返回数据:", res.data);
						}
					},
					fail: (err) => {
						console.error("上传失败:", err);
					}
				});
			},
			uploadPhoto() {
				uni.showActionSheet({
					itemList: ['拍照', '从相册选择'],
					success: (res) => {
						const sourceType = res.tapIndex === 0 ? ['camera'] : ['album'];

						uni.chooseImage({
							count: 1,
							sizeType: ['original', 'compressed'],
							sourceType: sourceType,
							success: (res) => {
								const tempFilePaths = res.tempFilePaths; // 临时文件路径数组
								console.log("选择的图片路径:", tempFilePaths);
								this.uploadFile(tempFilePaths[0]); // 上传第一张图片
							},
							fail: (err) => {
								console.error('选择图片失败', err);
								uni.showToast({
									title: '选择图片失败',
									icon: 'none'
								});
							}
						});
					}
				});
			},

			// 备选方案：使用uni.uploadFile上传文件

			addAnswer() {
				// if (this.list.length == 0) {
				// 	return this.$msg('请上传图片后尝试')
				// }
				this.showInp = true
			},
			createTask() {
				if (this.setAct == -1) {
					uni.showToast({
						title: '请先选择班级',
						icon: 'none'
					});
					return;
				}
				this.show = true
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
		background-color: #f5f5f5;
		padding: 40rpx;
		padding-bottom: 200rpx;
		overflow-y: auto;

	}

	.inputStr {
		border: 2rpx solid #ccc;
		height: 80rpx;
		border-radius: 15rpx;
		padding-left: 20rpx;
	}

	.popW {
		width: 700rpx;

		.top {
			width: 100%;
			height: 104rpx;
			background: #C8FFE2;
			padding: 10rpx 30rpx;

			.tit {
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: bold;
				text-align: center;
				font-size: 36rpx;
				color: #000000;
			}

			.botR {
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				font-size: 24rpx;
				text-align: right;
				color: #333333;
			}
		}

		.boxF {
			background-color: #fff;
			padding: 30rpx;

			.btn {
				margin-top: 60rpx;

				.lefbtn {
					width: 212rpx;
					height: 68rpx;
					background: #CCCCCC;
					border-radius: 45rpx;
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
				}

				.rig {
					font-weight: 500;
					font-size: 28rpx;
					color: #FFFFFF;
					width: 208rpx;
					height: 68rpx;
					margin-left: 50rpx;
					background: #05C490;
					border-radius: 45rpx;
				}
			}

			.tinD {
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 500;
				font-size: 32rpx;
				margin-bottom: 30rpx;
				color: #000000;
			}

			.fcen {
				// width: 22%;

				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 500;
				font-size: 28rpx;
				padding: 10rpx 40rpx;
				border-radius: 44rpx;
				background-color: #FDE5E5;
				color: #333333;
				margin-right: 3%;
				margin-bottom: 20rpx;

				// &:nth-child(4n) {
				// 	margin-right: 0;
				// }
			}
		}
	}

	.task-card {
		background: linear-gradient(90deg, #94EFD6 0%, #DCFFE0 100%);
		border-radius: 40rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		position: relative;
		overflow: hidden;
		position: relative;
	}

	.task-header {
		margin-bottom: 20rpx;
	}

	.task-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #2c3e50;
	}

	.task-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.task-info {
		flex: 1;
	}

	.info-item {
		display: flex;
		margin-bottom: 10rpx;
	}

	.info-label {
		font-size: 28rpx;
		color: #34495e;
		margin-right: 10rpx;
		font-weight: 500;
	}

	.info-value {
		font-size: 28rpx;
		color: #2c3e50;
	}

	.task-icon {
		width: 120rpx;
		height: 208rpx;
		// opacity: 0.3;
	}

	.icon-img {
		width: 100%;
		height: 100%;
	}

	.section {
		margin-bottom: 40rpx;
	}

	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #2c3e50;
		margin-bottom: 20rpx;
	}

	.class-selection {
		display: flex;
		flex-wrap: wrap;
		// gap: 20rpx;
	}

	.class-btn {
		width: 48%;
		margin-right: 2%;
		height: 120rpx;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
		display: flex;
		font-size: 28rpx;
		font-weight: 500;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;
		border: 2rpx solid #ccc;
		color: #000;

		&:nth-child(2n) {
			margin-right: 0;
		}
	}

	.class-btn-1 {
		background: #64D2FD;
		color: #fff;
	}

	.class-btn-2 {
		// background: #64D2FD;
	}

	.class-icon {
		width: 74rpx;
		height: 68rpx;
		margin-right: 30rpx;
	}



	.upload-section {
		display: flex;
		gap: 20rpx;
		margin-bottom: 30rpx;
	}

	.upload-btn {
		// flex: 1;
		width: 85%;
		height: 90rpx;
		border-radius: 30rpx;
		border: 4rpx solid #6F7E9F;
		background-color: white;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.upload-icon {
		width: 52rpx;
		height: 48rpx;
		margin-right: 34rpx;
	}

	.upload-text {
		font-size: 26rpx;
		color: #666;
	}

	.add-btn {
		width: 76rpx;
		height: 76rpx;
		background-color: white;
		border-radius: 38rpx;
		display: flex;
		font-weight: bold;
		align-items: center;
		justify-content: center;
		// box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
		border: 4rpx solid #4A5C85;
	}

	.add-text {
		font-size: 40rpx;
		color: #666;
		font-weight: 300;
	}

	.answer-grid {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		gap: 20rpx;
	}

	.answer-item {
		height: 80rpx;
		background-color: #f8f9fa;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		border: 2rpx solid #e9ecef;
	}

	.answer-text {
		font-size: 26rpx;
		color: #495057;
	}

	.answer-badge {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
		width: 32rpx;
		height: 32rpx;
		background-color: #ff4757;
		border-radius: 50%;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.badge-text {
		font-size: 20rpx;
		color: white;
		font-weight: bold;
	}

	.create-section {
		position: fixed;
		bottom: 0rpx;
		height: 150rpx;
		background-color: #f5f5f5;
		z-index: 99999;
		width: 90%;
		margin-top: 60rpx;
		// padding-bottom: 40rpx;
	}

	.create-btn {
		width: 100%;
		height: 88rpx;
		background: #05C490;
		color: white;
		border: none;
		border-radius: 44rpx;
		font-family: SourceHanSansCN, SourceHanSansCN;
		font-weight: 500;
		font-size: 36rpx;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.create-btn::after {
		border: none;
	}

	/* 拖拽时的网格动画 */
	.answer-grid {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		gap: 20rpx;
		transition: all 0.3s ease;
	}

	/* 长按删除提示 */
	.answer-item::before {
		content: '';
		position: absolute;
		top: -5rpx;
		left: -5rpx;
		right: -5rpx;
		bottom: -5rpx;
		border: 2rpx dashed transparent;
		border-radius: 45rpx;
		transition: all 0.3s ease;
	}

	.answer-item:active::before {
		border-color: #ff4757;
		animation: pulse 1s infinite;
	}

	@keyframes pulse {
		0% {
			opacity: 0.5;
		}

		50% {
			opacity: 1;
		}

		100% {
			opacity: 0.5;
		}
	}




	.dragging {
		opacity: 0.5;
	}

	.drag-indicator {
		position: absolute;
		left: 10rpx;
		top: 10rpx;
		color: #999;
	}

	.drag-dots {
		font-size: 24rpx;
	}
</style>